// home/pay/pay.js
import api from '../../utils/api.js';
import network from '../../utils/network.js';
import notice from '../../utils/notice.js';
import utils from '../../utils/utils';

const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        style: app.globalData.style,
        site: {},
        user: {},
        timer: '',
        list: [],
        amount: 0,
        name: '',
        mobile: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let list = JSON.parse(decodeURIComponent(options.timer));
        let amount = 0
        for (let i = 0; i < list.length; i++) {
            amount += list[i].amount;
        }
        this.setData({
            site: JSON.parse(decodeURIComponent(options.site)),
            list,
            amount: amount.toFixed(2),
            user: app.globalData.userInfo,
            name: app.globalData.userInfo.name,
            mobile: app.globalData.userInfo.mobile
        })

        notice.addNotice("UserInfo", this.refreshUserInfo, this);
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        notice.removeNotice("UserInfo", this);
    },

    refreshUserInfo() {
        let userInfo = app.globalData.userInfo;
        this.setData({
            name: userInfo.name,
            mobile: userInfo.mobile
        })
    },

    nameInput(e) {
        this.data.name = e.detail.value;
    },

    mobileInput(e) {
        this.data.mobile = e.detail.value;
    },

    goInfo() {
        wx.navigateTo({
            url: '/myself/user/info',
        })
    },

    cancelTap(e) {
        const index = e.currentTarget.dataset.index;
        this.data.list.splice(index, 1);

        let amount = 0;
        for (let i = 0; i < this.data.list.length; i++) {
            amount += this.data.list[i].amount;
        }

        this.setData({
            amount: amount.toFixed(2),
            list: this.data.list
        })

        notice.postNotice('GetTimer', this.data.list)
    },

    getUserInfo() {
        wx.getUserProfile({
            desc: '用于完善用户资料',
            success: (res) => {
                this.setData({
                    name: res.userInfo.nickName
                })
            }
        })
    },

    getRealtimePhoneNumber(e) {
        network.getJson(api.getUserPhoneNumber, {
            userId: this.data.user.id,
            code: e.detail.code
        }).then(res => {
            this.setData({
                mobile: res.data
            })
        })
    },

    onSubmit(e) {
        this.name = e.detail.value.name;
        utils.fromEvent().then(() => {
            if (!this.data.name) {
                wx.showToast({
                    icon: 'none',
                    // title: '预订人不能为空',
                    title: '请完善预订人名称',
                })
                return;
            }
            if (!utils.isTelNum(this.data.mobile)) {
                wx.showToast({
                    icon: 'none',
                    // title: '请输入正确的手机号',
                    title: '请完善预订人手机号',
                })
                return;
            }
            if (this.data.list.length == 0) {
                wx.showToast({
                    icon: 'none',
                    title: '请选择场次',
                })
                return;
            }
            for (let i = 0; i < this.data.list.length; i++) {
                this.data.list[i].timer = this.data.list[i].mode;
                this.data.list[i].id = null;
            }
            network.postJson(api.createOrder, {
                userId: this.data.user.id,
                userName: this.data.name,
                mobile: this.data.mobile,
                siteId: this.data.site.id,
                siteMode: this.data.site.mode,
                bookingTimes: this.data.list,
                amount: this.data.amount,
                type: 1,
                payMode: 1
            }).then(res => {
                this.orderId = res.data.orderId;
                return network.postJson(api.prepay, {
                    description: this.data.site.name,
                    outTradeNo: res.data.orderNo,
                    amount: this.data.amount,
                    openid: this.data.user.openId,
                    orderType: "2" // 1=活动，2=场地
                })
            }).then(data => {
                let result = data.data;
                wx.requestPayment({
                    timeStamp: result.timeStamp,
                    nonceStr: result.nonceStr,
                    package: result.packageVal,
                    signType: result.signType,
                    paySign: result.paySign,
                    success: res => {
                        wx.reLaunch({
                            url: '/home/success/success?mode=PAY&orderId=' + this.orderId + '&siteId=' + this.data.site.id + '&accommodate=' + this.data.site.accommodate
                        })
                    },
                    fail(res) {
                        console.log(res)
                    }
                })
            })
        })
    }
})